package com.example.webdome.controller;

import com.example.webdome.pojo.Label;
import com.example.webdome.pojo.Result;
import com.example.webdome.service.LabelService;
import com.example.webdome.util.getCookie;
import com.example.webdome.util.urlCount;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/Labels")
public class LabelController {
    @Resource
    private LabelService labelService;
    @Resource
    private getCookie getCookie;
    @Resource
    private urlCount Count;
    //自定义新域名接口
    @PostMapping("/addLabel")
    public Result saveLabel(@RequestBody Label label, HttpServletRequest request) {
        String uri = request.getRequestURI();
        Count.recordUrlCount(uri);
        String cookieValue = request.getHeader("Cookie");
        String token = getCookie.getCookieValue(cookieValue);
        try {
            if (token != null) {
                return labelService.saveLabel(label, token);
            }
        } catch (Exception e) {
            return Result.failed("保存失败：" + e.getMessage());
        }
        return Result.failed("请先登录！");
    }
    //根据登录用户获取自定义域名
    @GetMapping("/getLabelByUser")
    public Result getLabelByUserName(HttpServletRequest request) {
        String cookieValue = request.getHeader("Cookie");
        String token = getCookie.getCookieValue(cookieValue);
        try {
            if (token != null) {
                return labelService.getLabelByUserName(token);
            }
        } catch (Exception e) {
            return Result.failed("加载失败！" + e.getMessage());
        }
        return Result.failed("请先登录！");
    }

}

